package com.zhu.aliyunService.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.zhu.aliyunService.service.videoService;
import com.zhu.aliyunService.util.VodProperties;
import com.zhu.aliyunService.util.VodUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class videoServiceImpl implements videoService {
    @Autowired
    private VodProperties vodProperties;
@Autowired
private VodUtils vodUtils;
    /**
     *
     * @param inputStream
     * @param originalFileName
     * @return
     */
    @Override
    public String uploadVideo(InputStream inputStream, String originalFileName) {
        String title = originalFileName.substring(0, originalFileName.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKeyId(),
                vodProperties.getAccessKeySecret(), title, originalFileName, inputStream);

        UploadVideoImpl uploadVideo=new UploadVideoImpl();

        UploadStreamResponse response = uploadVideo.uploadStream(request);
        log.info("RequestId:========>{}",response.getRequestId());
        String requestId = response.getVideoId();

        if(StringUtils.isEmpty(requestId)){
            log.error("阿里云视频上传失败！"+response.getCode()+response.getMessage());
        }else{
          log.info("阿里云视频上传成功！");
        }
        return requestId;
    }

    @Override
    public boolean delete(String id) throws Exception{
//        VodUtils vodUtils=new VodUtils();
//        vodUtils.
        DefaultAcsClient client = vodUtils.initClient();
//        DefaultAcsClient client = VodUtils.initClient();
        DeleteVideoResponse deleteVideoResponse = VodUtils.deleteVideoResponse(client, id);
        log.info("阿里云视频删除成功：{}",deleteVideoResponse.getRequestId());
        return true;
    }

    @Override
    public boolean deleteList(List<String> idList) throws ClientException {
        DefaultAcsClient client=vodUtils.initClient();
        DeleteVideoResponse deleteVideoResponse = VodUtils.deleteVideoResponse(client, idList);
        log.info("视频删除成功！(批量)");
        return true;
    }

    @Override
    public String getPlayAuth(String id) throws ClientException {
        DefaultAcsClient client = vodUtils.initClient();
        GetVideoPlayAuthResponse videoPlayAuthResponse = VodUtils.getVideoPlayAuthResponse(client, id);
        String playAuth = videoPlayAuthResponse.getPlayAuth();
        return playAuth;
    }
}
